package org.sky.axis2.javacomplextype;

import java.util.Iterator;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNode;
import org.apache.axis2.client.async.AxisCallback;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.apache.axis2.engine.DefaultObjectSupplier;

public class PersonCallBack implements AxisCallback {

	@SuppressWarnings("unchecked")
	public void onMessage(MessageContext msgContext) {
		OMElement result = msgContext.getEnvelope().getBody().getFirstElement();
		System.out.println("result====" + result);
		if (result == null) {
			return;
		}
		Iterator<OMNode> iterator = (Iterator<OMNode>) result
				.getChildElements();
		while (iterator.hasNext()) {
			OMNode omNode = (OMNode) iterator.next();
			if (omNode.getType() == OMNode.ELEMENT_NODE) {
				OMElement omElement = (OMElement) omNode;

				if (omElement.getLocalName().toLowerCase().equals("person")) {
					try {
						Person person = (Person) BeanUtil.processObject(
								omElement, Person.class, null, true,
								new DefaultObjectSupplier());
						System.out.println("person name=" + person.getName()
								+ " person age=" + person.getAge());
					} catch (Exception e) {
						System.out.println("-------Error Occured-------");
						e.printStackTrace();
					}
				}
			}
		}
		synchronized (this) {
			try {
				this.notify();
			} catch (Exception e) {
			}
		}
	}

	public void onFault(MessageContext msgContext) {
		System.out.println(msgContext.getEnvelope().getBody().getFault()
				.toString());
		synchronized (this) {
			try {
				this.notify();
			} catch (Exception e) {
			}
		}
	}

	public void onError(Exception e) {
		synchronized (this) {
			try {
				this.notify();
			} catch (Exception ex) {
			}
		}
		e.printStackTrace();
	}

	public void onComplete() {
		synchronized (this) {
			try {
				this.notify();
			} catch (Exception e) {
			}
		}
	}
}